<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :leftType="'1'" @clickLeftBack="clickLeftBack"
				:title="$t('whether.lvtBoardWorker')" scanType="0"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="barcodeSplitting" :style="'height: calc(100vh - ' + navHeight + 'px);'">
				<view class="custom-form-card">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('translationImage.materialsInformation') }}
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('distributedEnter.materialCode')
						}}：</text>
						<comboGrid ref="comboGridRef" v-model="data.materialCode" :requestParam="data.requestParam"
							requestUrl="/api1/wms/commonUtil/QueryMaterial" :calculate="false"
							:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="locationCode"
							:column="data.columnRule.column" bindValue="locationName" @setClick="getRuleClick">
						</comboGrid>
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0;">
						<text class="item-info-title" style="width: 180rpx;">{{ $t('distributedEnter.materialName')
						}}：</text>
						<uni-easyinput class="uni-mt-5" :placeholder="$t('whether.automaticallyCarriedOut')"
							v-model="data.materialName" disabled></uni-easyinput>
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0;">

						<text class="item-info-title" style="width: 180rpx;">{{ $t('distributedEnter.materialSpec')
						}}：</text>
						<uni-easyinput class="uni-mt-5" :placeholder="$t('whether.automaticallyCarriedOut')"
							v-model="data.materialSpec" disabled></uni-easyinput>
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0;">
						<text class="item-info-title" style="width: 180rpx;">
							<text class="required-color">*</text>{{ $t('distributedEnter.qty') }}：
						</text>
						<uni-easyinput class="uni-mt-5" v-model="data.qty" @blur="changeQty"
							:placeholder="$t('distributedEnter.pleaseMaterialQty')"></uni-easyinput>
						<text style="margin-left: 20rpx;color: brown;font-weight: 600;">kg</text>
					</view>
				</view>
				<!-- 底部确定按钮 -->
				<view class="page-bottom-box">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="confirmFn">{{ $t('calendar.submit')
					}}
					</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>

<script setup>
import { navigate, navigateBack } from '@/utils/routerSkip.js';
import pdaScan from '@/hooks/pdaScan.js'
import { useStore } from "vuex";
import { DEFAULT_WAREHOUSECODE, DEFAULT_WAREHOUSEID, OPERATION_COMMIT, OPERATION_SCAN } from '@/constant/index.js'
import { getDataDictionaryFn } from '@/utils/index.js'
import storageBin from '@/components/storageBin.vue'
import comboGrid from '@/components/comboGrid.vue'
import { onShow } from '@dcloudio/uni-app';
import { basPdaOperationLogFn } from '@/utils/index.js';
import {lvtBoardWorker } from '@/api/api.js'
import { ref, reactive, onMounted, watch, computed, toRaw } from 'vue';
import { onBackPress } from '@dcloudio/uni-app';

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

components: {
	storageBin,
		comboGrid
}
const storageBinRef = ref(null)
const comboGridRef = ref(null)
let store = useStore();
const navHeight = computed(() => {
	return store.state.navHeight
})

const data = reactive({
	scanInfo: undefined,
	requestParam: {
		queryWord: "",
		pageType: "",
		materialType: '4319MF'
	},
	columnRule: {
		column: [{
			label: t('distributedEnter.materialCode'),
			name: "materialCode",
		}, {
			label: t('distributedEnter.materialName'),
			name: "materialName",
		}, {
			label: t('distributedEnter.materialSpec'),
			name: "specification",
		}]
	},
	materialCode: '',
	materialName: '',
	materialSpec: '',
	qty: null
})


// 下拉选择
const getRuleClick = (x) => {
	// 选择赋值
	data.materialCode = x.materialCode
	data.materialName = x.materialName
	data.materialSpec = x.specification
	data.scanInfo = x
}

const changeQty = () => {
	data.scanInfo.qty = data.qty
}
// 提交
const confirmFn = () => {
	if(!data.scanInfo){
		uni.showToast({
			title: t('whether.materialInformation'),
			icon: 'none'
		})
		return 
	}
	if(!data.qty||data.qty<=0){
		uni.showToast({ title: t('Common.inputNumber'), icon: "none" });
		return
	}
	
	lvtBoardWorker({"depeCode":"BM000074","materialCode":data.materialCode,"qty":data.qty}).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '开槽灰吨包投料'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code, //返回状态
				errorMessage: res.msg, //错误信息
				isPass: res.code == 200 ? 'Y' : 'N', //是否成功 Y N
				operationType: OPERATION_COMMIT,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {}
		if (res.ok == true) {
			data.materialCode = ''
			data.materialName = ''
			data.materialSpec = ''
			data.qty = ''
			data.scanInfo = undefined
			uni.showToast({
				title:t('smashRemoval.submitSuccessfully'),
				icon: "none"
			});
		
	
		} else {
			uni.showToast({
				title: res.msg,
				icon: "none"
			});
		}
	})
}

//导航栏左侧返回
const clickLeftBack = () => {
	if (!data.scanInfo) {
		uni.navigateBack({
			delta: 1
		});
		return true
	}
	uni.showModal({
		title: t('smashRemoval.warmPrompt'),
		content: t('whether.dataNotSubmitted'),
		success: function (res) {
			if (res.confirm) {
				data.materialCode = ''
				data.materialName = ''
				data.materialSpec = ''
				data.qty = ''
				data.scanInfo = undefined
				uni.navigateBack({
					delta: 1
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
onBackPress((options) => {
	if (options.from == 'backbutton') {
		if (!data.scanInfo) {
			uni.navigateBack({
				delta: 1
			});
			return true
		}
		uni.showModal({
			title: t('smashRemoval.warmPrompt'),
			content: t('whether.dataNotSubmitted'),
			success: function (res) {
				if (res.confirm) {
				data.materialCode = ''
				data.materialName = ''
				data.materialSpec = ''
				data.qty = ''
					data.scanInfo = undefined
					uni.navigateBack({
						delta: 1
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		return true;
	}
})

</script>


<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.page-bottom-box {
	position: absolute;
	left: 0px;
	bottom: 0px;
	width: 100%;
}
</style>